package com.dhnsoft.代码随想录.双指针法;


import java.util.List;

/**
 * ClassName: d03_206_反转链表
 * Package: com.dhnsoft.代码随想录.双指针法
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/20 11:28
 * @Version 1.0
 */
public class d03_206_反转链表 {

    class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        ListNode virtualHead = new ListNode(-1);
        ListNode newList = new ListNode(-1);
        virtualHead.next = head;
        ListNode cur = virtualHead.next;
        ListNode temp = null;
        while (cur != null) {
            temp = cur.next;
            cur.next = newList.next;
            newList.next = cur;
            cur = temp;
        }
        return newList.next;
    }
}


